package com.atuan.datepickerlibrary;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by atuan on 2018/9/1.
 */

public class CalendarUtil {


    public static String getTwoDay(String sj1, String sj2) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        long day = 0;
        try {
            java.util.Date date = myFormatter.parse(sj1);
            java.util.Date mydate = myFormatter.parse(sj2);
            day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            return "";
        }
        return day + "";
    }


    /**
     * 判断当前日期是星期几
     *
     * @param pTime 设置的需要判断的时间  //格式如2012-09-08
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
    public static int getWeekNoFormat(String pTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return c.get(Calendar.DAY_OF_WEEK);
    }

    public static Calendar toDate(String pTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
            return c;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 参数格式：2012-12-1
     * return 12月1日
     *
     * @param date
     */
    public static String FormatDateMD(String date) {
        if (TextUtils.isEmpty(date)) {
            new Throwable();
        }
        String month = date.split("-")[1];
        String day = date.split("-")[2];
        return month + "月" + day + "日";
    }

    /**
     * 参数格式：2012-12-1
     * return 12-1日
     *
     * @param date
     */
    public static String FormatDate_MD(String date) {
        if (TextUtils.isEmpty(date)) {
            new Throwable();
        }
        String month = date.split("-")[1];
        if (Integer.parseInt(month)<=9){
            month="0"+month;
        }
        String day = date.split("-")[2];
        if (Integer.parseInt(day)<=9){
            day="0"+day;
        }
        return month + "-" + day + "";
    }


    /**
     * 保留两位小数
     */
    public static String double2String(String dataString) {
        try {
            double dataDouble = Double.parseDouble(dataString);
            DecimalFormat df = new DecimalFormat("######0.00");
            String format = df.format(dataDouble);
            return format;
        } catch (Exception e) {
            e.printStackTrace();
            return "default";
        }
    }

    /**
     * 参数格式：2012-12-1
     * return 2012年12月1日
     *
     * @param date
     */
    public static String FormatDateYMD(String date) {
        if (TextUtils.isEmpty(date)) {
            new Throwable();
        }
        String year = date.split("-")[0];
        String month = date.split("-")[1];
        String day = date.split("-")[2];
        return year + "年" + month + "月" + day + "日";
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 设置的需要判断的时间  //格式如2012-09-08
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
    @SuppressLint("SimpleDateFormat")
    public static String getWeekByFormat(String pTime) {
        String week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            week += "日";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            week += "六";
        }
        return week;
    }


    /**
     * 参数格式：long
     * return 12月1日
     *
     * @param dateTime
     */
    public static String FormatDateLong(long dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm分ss秒");
        Date date = new Date(dateTime);
        return simpleDateFormat.format(date);
    }


    /**
     * 参数格式：long
     * return 12月1日
     *
     * @param dateTime
     */
    public static long FormatStringLong(String dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        try {
            Date parse = simpleDateFormat.parse(dateTime);
            long time = parse.getTime();
            return time;
        } catch (ParseException e) {

        }
        return 0;
    }


    /**
     * 参数格式：long
     * return 12月1日
     *
     * @param dateTime
     */
    public static String FormatDateString(long dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(dateTime);
        return simpleDateFormat.format(date);
    }



    public static boolean DataBoolean(long dateTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        Date date = new Date(dateTime);
        String format = simpleDateFormat.format(date);
        try{
            int i = Integer.parseInt(format);
            if (i>16){
                return true;
            }else {
                return false;
            }
        }catch (Exception E){
            return false;
        }
    }


    /**
     * yyyy-MM-dd  -> mm月dd日
     *
     * @param dateTime
     * @return
     */
    public static String FormatChangeString(String dateTime) {
        if (dateTime.length() != 10) {
            return "null";
        }
        String substring = dateTime.substring(5);
        return substring.replace("-", "月") + "日";
    }

    public static int getDateDays(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(date1);// 通过日期格式的parse()方法将字符串转换成日期
            Date dateBegin = sdf.parse(date2);
            long betweenTime = date.getTime() - dateBegin.getTime();
            betweenTime = betweenTime / 1000 / 60 / 60 / 24;
            return (int)betweenTime;
        } catch (Exception e) {
        }
        return 1;
    }
}
